package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.dao.IContactAddressDao;
import com.eling.elcms.basedoc.dao.IPersonalInfoDao;
import com.eling.elcms.basedoc.model.ContactAddress;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.member.dao.IMemberDao;
import com.eling.elcms.member.dao.IMemberPointDao;
import com.eling.elcms.member.model.Member;
import com.eling.elcms.member.model.MemberPoint;
import com.eling.elcms.travel.dao.ICertificatesTransferDao;
import com.eling.elcms.travel.dao.ICostPriceDao;
import com.eling.elcms.travel.model.CertificatesTransfer;
import com.eling.elcms.travel.model.CertificatesTransfer.TransferType;
import com.eling.elcms.travel.model.CostPrice;
import com.eling.elcms.travel.model.CostPrice.FlatCostStatus;
import com.eling.elcms.travel.model.view.CertificateTransferView;
import com.eling.elcms.travel.service.ICertificatesTransferManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CertificatesTransferManagerImpl extends GenericManagerImpl<CertificatesTransfer, Long> implements ICertificatesTransferManager{
	ICertificatesTransferDao certificatesTransferDao;

	@Autowired
	private IMemberDao memberDao;
	@Autowired
	private IPersonalInfoDao personalInfoDao;
	@Autowired
	private IContactAddressDao contactAddressDao;
	@Autowired
	private IMemberPointDao memberPointDao;
	@Autowired 
	private ICostPriceDao costPriceDao;
	
	@Autowired
	public void setBasePriceDao(ICertificatesTransferDao certificatesTransferDao) {
		this.dao = certificatesTransferDao;
		this.certificatesTransferDao = certificatesTransferDao;
	}

	@Override
	public List<CertificateTransferView> queryNotes(Date operateDate, Date operateDateEnd) {
		
		return certificatesTransferDao.queryNotes(operateDate, operateDateEnd);
	}

	@Override
	public List<CertificateTransferView> queryNotess(Date operateDate, Date operateDateEnd,
			Long pkCertificatesTransfer) {
		
		return certificatesTransferDao.queryNotess( operateDate,  operateDateEnd,  pkCertificatesTransfer);
	}

	@Override
	public CertificatesTransfer saveAll(CertificatesTransfer certificatesTransfer,ContactAddress contactAddress) {
		Member receiver = certificatesTransfer.getReceiver();
		if(receiver.getPkMember()==null){
			receiver.setPersonalInfo(personalInfoDao.save(receiver.getPersonalInfo()));
			contactAddress.setPersonalInfo(receiver.getPersonalInfo());
			contactAddress.setDeleteFlag(false);
			contactAddressDao.save(contactAddress);
			receiver = memberDao.save(receiver);
			
			MemberPoint memberPoint = new MemberPoint();
			memberPoint.setVersion(0);
			memberPoint.setPkMember(receiver.getPkMember());
			memberPoint.setPkServicePoint(certificatesTransfer.getTravelContract().getServicePoint().getPkServicePoint());
			memberPointDao.save(memberPoint);
		}
		
		Member assignor = certificatesTransfer.getTravelContract().getMember();
		certificatesTransfer.setReceiver(receiver);
		certificatesTransfer.setAssignor(assignor);
		certificatesTransfer.getTravelContract().setMember(receiver);
		
		//若是类型为转让时，给工本费推送数据
		if(certificatesTransfer.getTransferType()==TransferType.Transfer){
			CostPrice costPrice = new  CostPrice();
			costPrice.setTravelContract(certificatesTransfer.getTravelContract());
			costPrice.setFlatCost(certificatesTransfer.getWorkCost());
			costPrice.setFlatCostStatus(FlatCostStatus.Nocharge);
			costPrice.setOperateDate(certificatesTransfer.getOperateDate());
			costPrice.setCause(certificatesTransfer.getRemark());
			costPrice.setVersion(0);
			costPriceDao.save(costPrice);
		}
		
		return this.save(certificatesTransfer);
	}

	@Override
	public List<Member> searchMember(SearchCondition sc) {
		List<Member> list = memberDao.search(sc);
		List<Long> pkList = new ArrayList<Long>();
		for(Member m : list){
			pkList.add(m.getPkMember());
		}
		CertificatesTransfer ct = new CertificatesTransfer();
		PropertyUtils.setProperty(ct, "assignor.pkMemberIn", pkList);
		List<CertificatesTransfer> certificatesTransfers = certificatesTransferDao.query(ct);
		for(CertificatesTransfer c : certificatesTransfers){
			 for (Iterator<Member> iter = list.iterator(); iter.hasNext();) {
				 Member member = (Member) iter.next();
				 if(c.getAssignor().getPkMember().equals(member.getPkMember())){
					 	iter.remove();
					}
				 }
		}
		
		return list;
	}

}
